package com.ihrm.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.domain.company.Company;
import com.ihrm.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//解决跨域问题
@CrossOrigin
@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    CompanyService companyService;

    /**
     * 添加企业
     * @param company
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result add(@RequestBody Company company){
        companyService.add(company);
        return Result.SUCCESS();
    }

    /**
     * 根据id更新企业信息
     * @param id
     * @param company
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "id") String id, @RequestBody Company company){
        Company com = companyService.findById(id);
        com.setName(company.getName());
        com.setRemarks(company.getRemarks());
        com.setState(company.getState());
        com.setAuditState(company.getAuditState());
        companyService.update(company);
        return Result.SUCCESS();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(name = "id") String id){
        companyService.deleteById(id);
        return Result.SUCCESS();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(name = "id") String id){
        Company company = companyService.findById(id);
        return new Result(ResultCode.SUCCESS, company);
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result findAll(){
        List<Company> companyList = companyService.findAll();
        return new Result(ResultCode.SUCCESS, companyList);
    }
}
